/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: IndexController
 * Author:   zhouheng
 * Date:     2019/4/17 22:57
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.portal.controller;

import com.dubbo.user.api.IUserCoreService;
import com.dubbo.user.api.dto.UserLoginRequest;
import com.dubbo.user.api.dto.UserLoginResponse;
import com.dubbo.user.api.dto.UserRegisterRequest;
import com.dubbo.user.api.dto.UserRegisterResponse;
import com.portal.support.BaseController;
import com.portal.support.ResponseData;
import com.portal.support.ResponseEnum;
import com.portal.support.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/4/17
 * @since 1.0.0
 */
@Controller
public class IndexController extends BaseController {
    @Autowired
    IUserCoreService userCoreService;

    @GetMapping("/index")
    public String index(Model model) {
        System.out.println("test");
        return "index";
    }

    @GetMapping("/login")
    @Anonymous
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    @Anonymous
    public ResponseEntity login(String txtUser, String Userpwd, HttpServletResponse response) {
        UserLoginRequest request = new UserLoginRequest();

        request.setPassword(Userpwd);
        request.setUsername(txtUser);
        UserLoginResponse userLoginResponse = userCoreService.login(request);
        if("000000".equals(userLoginResponse.getCode())){
            response.addHeader("Set-Cookie","access_token="+userLoginResponse.getToken()+";Path=/;HttpOnly");
        }

        return ResponseEntity.ok(userLoginResponse);
    }

    @PostConstruct
    @Anonymous
    public @ResponseBody ResponseData register(String username,String password,String mobile){
        ResponseData data = new ResponseData();

        UserRegisterRequest request=new UserRegisterRequest();
        request.setMobile(mobile);
        request.setUsername(username);
        request.setPassword(password);

        try {
            UserRegisterResponse response = userCoreService.register(request);

            if(response.getCode().equals("000000")){
                //发送邮件  发送卡券
            }

            data.setMessage(response.getMsg());
            data.setCode(response.getCode());
        }catch(Exception e) {
            data.setMessage(ResponseEnum.FAILED.getMsg());
            data.setCode(ResponseEnum.FAILED.getCode());
        }

        return data;
    }

}